#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 下载美拍视频

import os
import requests
import bs4


def parse(url):
    r = requests.get(url)
    soup = bs4.BeautifulSoup(r.text)
    meta = soup.find('meta', attrs={'property': 'og:video:url'})
    return meta.get('content')


def down(url):
    fname = os.path.basename(url)
    r = requests.get(url, stream=True)
    chunk_size = 1024
    with open(fname, "wb") as file:
        down_size = 0
        total_size = int(r.headers.get('content-length', 0))
        for data in r.iter_content(chunk_size=chunk_size):
            down_size += len(data)
            print '%d/%d\r' % (down_size, total_size),
            file.write(data)


urls = [
    'http://www.meipai.com/media/15828042',
    'http://www.meipai.com/media/15831348',
    'http://www.meipai.com/media/15846876',
    'http://www.meipai.com/media/15847979',
    'http://www.meipai.com/media/29013091',
    'http://www.meipai.com/media/43536283',
    'http://www.meipai.com/media/196018765',
    'http://www.meipai.com/media/210672418',
    'http://www.meipai.com/media/253898196',
    'http://www.meipai.com/media/324278855',
    'http://www.meipai.com/media/365299609',
    'http://www.meipai.com/media/367250087',
    'http://www.meipai.com/media/455171265',
    'http://www.meipai.com/media/484803353',
]

for i, url in enumerate(urls):
    print "[%d/%d] %s" % (i, len(urls), url)
    link = parse(url)
    down(link)
